(function(app){
	app
		.controller("content_12_42_inputCtrl",["$scope","$state","myFactory",'$stateParams',"$q",'$timeout','$location','$http','$ionicHistory',function($scope,$state,myFactory,$stateParams,$q,$timeout,$location,$http,$ionicHistory){

			//接收参数
			var itemId = $stateParams.changeTypeId;//变更项目ID
			var chdrnum01 =  $stateParams.chdrnum01; //保单号
			var chdrcoy = $stateParams.chdrcoy; //保单公司号码
			var initFlag = false;//检测标志，在初始化函数完成后，会被置为true
			var servername42 = "";
			var servername12 = "";
			var token=sessionStorage.getItem("token");
			$scope.chdrnum01=$stateParams.chdrnum01; //保单号
			$scope.showTbrFlag_item42 = true;//默认是显示投保人选项的
			$scope.showBbrFlag_item12 = true;//默认是显示被保人选项的
			$scope.data42_checkFlag = false;
			$scope.data12_checkFlag = false;

			$scope.data = {
				//checkFlag: false
			};

/**********************************************初始化函数begin****************************************************/
			//初始化查询变更项内容
			$scope.load = function () {
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-token-timestamp";
				var signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				$http({
					method: 'GET',
					url: app.domain + 'input/initContentItem.action',
					params: {'itemId': itemId,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						$scope.chdrnum01=response.data.data.policy.base.chdrnum01;
						$scope.item42=response.data.data.modelMap.item42;
						$scope.item12=response.data.data.modelMap.item12;
						//servername42=response.data.data.modelMap.item42.servername;
						//servername12=response.data.data.modelMap.item12.servername;
						var item42=response.data.data.modelMap.item42;
						var item12=response.data.data.modelMap.item12;
						if((item42!=undefined && item42!="")){//投保人
							servername42=item42.servername;
						}
						if((item12!=undefined && item12!="")){//被保险人
							servername12=item12.servername;
						}

						//竟然有空格！！！MD    "VIP金卡           "
						if(""!=servername42 && undefined!=servername42){
							servername42=servername42.trim();
						}
						if(""!=servername12 && undefined!=servername12){
							servername12=servername12.trim();//"VIP金卡           "
						}

						//投/被保险人为同一人时，则只显示投保人信息
	                    //投/被保险人为同一人时，则只显示投保人勾选栏位
						//投保人证件号码
						var b=response.data.data.policy.base.secuityno;
						//被保人证件号码
						var b1=response.data.data.policy.base.rnIdNumber;
						//var b2=response.data.data.modelMap.item42.servername;//投保人服务卡名字
						//var b3=response.data.data.modelMap.item12.servername;//被保人服务卡名字
						var b2=servername42;
						var b3=servername12;
						if(b==b1){
							$scope.showBbrFlag_item12 = false;//隐藏被保人选项
						}
						if(b2==null || b2==""){
							$("#tbrInput").attr("disabled",true);
							$scope.item42.servername = "无";
							//document.getElementById("42servername").innerText="无";
							//$("#policy").attr("disabled",true);
						}
						if(b3==null || b3==""){
							$("#bbrInput").attr("disabled",true);
							$scope.item12.servername = "无";
							//document.getElementById("12servername").innerText="无";
							//$("#inSurant").attr("disabled",true);
						}
						if((b2==null || b2=="")&&(b3==null || b3=="")){
							$("#next").addClass("disabled");
						}

						initFlag=true;//标识初始化成功
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","初始化查询变更项内容失败!","info");
				});
			}
/**********************************************初始化函数end****************************************************/

			//重新受理
			$scope.renew = function(){
				myFactory.renew();
			}	
			//退出移动保全
			$scope.exit = function(){
				myFactory.exit();
			}			
			//返回上一页
			$scope.goBack = function(){
				myFactory.goBack();
			}

			//保单详情
			$scope.showPolicyDetail = function(){
				modal('open','policyModalOn12_42Input','policyWindowOn12_42Input','policyCloseXOn12_42Input');
				var timestamp=myFactory.getTimetamp();
				var sfis= "chdrnum-chdrcoy-token-timestamp";
				var signStr="chdrnum="+chdrnum01+"&chdrcoy="+chdrcoy+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化查询订单详情
				var policyData;
				$http({
					method: 'GET',
					url: app.domain + 'query/restFindPolicyDetail.action',
					params: {'chdrnum': chdrnum01,'chdrcoy': chdrcoy,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						policyData = response.data.data;
						$scope.policyData = policyData;
						$scope.policyData.ptdate01=myFactory.DateTools($scope.policyData.ptdate01);
					}
				}, function errorCallback(response) {
					$.alert({
						title: '',
						text: '初始化查询订单详情失败!',
						onOK: function () {
							//点击确认
						}
					});
				});
			}

			//保单详情弹窗关闭
			$scope.policyCancelOn12_42Input = function(){
				modal('close','policyModalOn12_42Input','policyWindowOn12_42Input','policyCloseXOn12_42Input');
			}

			//关闭遮罩
			$scope.overwindowClose = function(){
				//$(".overwindow").show();
				$(".overwindow").hide();
				$(".overmodal").hide();
			}
			
			//checkbox选择
			$scope.isCheck42 = function(val){
				if (val) {
					$scope.data42_checkFlag = false;
				} else{
					$scope.data42_checkFlag = true;
				};
				console.log("data42_checkFlag:"+$scope.data42_checkFlag);
			}
			$scope.isCheck12 = function(val){
				if (val) {
					$scope.data12_checkFlag = false;
				} else{
					$scope.data12_checkFlag = true;
				};
				console.log("data12_checkFlag:"+$scope.data12_checkFlag);
			}

			//下一步
			$scope.nextStep = function(){
				//判断是否初始化完成了，如果失败，不予进入下一步
				if(!initFlag){
					myFactory.alert("","初始化时失败，请返回上一步重试","info");
					return;
				}

				var radioFlag42=$scope.data42_checkFlag;
				var radioFlag12=$scope.data12_checkFlag;
				//var servername42=$scope.servername42;
				//var servername12=$scope.servername12;
				//var servername42 = $("#servername42").val();
				//var servername12 = $("#servername12").val();
				/*if(val.checked||va.checked){
					return true;
				}*/
				if (!radioFlag42 && !radioFlag12){
					$.alert({title: '',text: '请勾选投保人或者被保险人！',onOK: function () {}});
					return false;
				}
				var applicant="";
				var benefitperson="";
				if (radioFlag42){
					var applicant="on";
				}
				if (radioFlag12){
					var benefitperson="on";
				}
				var item={"applicant":applicant,"benefitperson":benefitperson,"servername42":servername42,"servername12":servername12};
				$ionicHistory.clearCache().then(function(){
					$state.go('content_check_main',{
						itemId:itemId,item:item,chdrnum01:$scope.chdrnum01,chdrcoy:chdrcoy
					});
				})
			}

		}]);

		//指针弹窗
		app.directive('infoid',function(){
			return {
				restrict: 'EA',		//指明指令在DOM里面以什么形式被声明,EA.表示即可以是元素也可以是属性
				templateUrl: 'html/input/content_base_info.html'+app.clearCache,
				replace: true,		//设置解析后，DOM声明的指令是否消失
				link: function(scope,element,attrs){  //链接函数负责注册DOM事件和更新DOM,
					console.log('指令完成');
				}
			}
		});

})(app)